#!/usr/bin/env bash

include "libs/ohos.sh"
include "libs/test.sh"

OHOS_DIR_GLOBAL_DB="$HOME/.config/ohos-tool"
OHOS_PATH_GLOBAL_DB="$OHOS_DIR_GLOBAL_DB/global.db"
function database:repo_load()
{
    ohos:cd_repo || return
    database:repo_set sync_time 0
    if [ -e "$(ohos:get_repo_path)/.ohos-tool.db" ]; then
        source "$(ohos:get_repo_path)/.ohos-tool.db"
    fi
    cd - 1>/dev/null
}

function database:repo_save()
{
    ohos:cd_repo || return
    declare |
        egrep "^database_repo_.*=.*" |
        egrep -v "^database_repo_test" > "$(ohos:get_repo_path)/.ohos-tool.db"
    cd - 1>/dev/null
}

function database:repo_get()
{
    #local key=$1
    local varname="database_repo_$1"
    echo ${!varname}
}

function database:repo_array_get()
{
    #local key=$1
    #local index=$2
    local varname="database_repo_$1"
    eval "echo \${$varname[$2]}"
}

function database:repo_set()
{
    #local key=$1
    #local val=$2
    local varname="database_repo_$1"
    eval "$varname=$2"
}

function database:repo_array_set()
{
    #local key=$1
    #local index=$2
    #local val=$3
    local varname="database_repo_$1"
    eval "$varname[$2]=$3"
}

function database:repo_inc()
{
    #local key=$1
    local varname="database_repo_$1"
    eval "$varname=$((${!varname} + 1))"
}

function database:repo_dec()
{
    #local key=$1
    local varname="database_repo_$1"
    eval "$varname=$((${!varname} - 1))"
}

function database:global_load()
{
    database:global_set docker_version 0
    database:global_set use_docker true
    database:global_set use_window true
    database:global_set git_update true
    database:global_set repo_number 0
    [ -e $OHOS_PATH_GLOBAL_DB ] && source $OHOS_PATH_GLOBAL_DB
}

function database:global_get()
{
    #local key=$1
    local varname="database_global_$1"
    echo ${!varname}
}

function database:global_array_get()
{
    #local key=$1
    #local index=$2
    local varname="database_global_$1"
    eval "echo \${$varname[$2]}"
}

function database:global_set()
{
    #local key=$1
    #local val=$2
    local varname="database_global_$1"
    eval "$varname=$2"
}

function database:global_array_set()
{
    #local key=$1
    #local index=$2
    #local val=$3
    local varname="database_global_$1"
    eval "$varname[$2]=$3"
}

function database:global_inc()
{
    #local key=$1
    local varname="database_global_$1"
    eval "$varname=$((${!varname} + 1))"
}

function database:global_dec()
{
    #local key=$1
    local varname="database_global_$1"
    eval "$varname=$((${!varname} - 1))"
}

function database:global_save()
{
    mkdir -p $OHOS_DIR_GLOBAL_DB
    declare |
        egrep "^database_global_.*=.*" |
        egrep -v "^database_global_test" > $OHOS_PATH_GLOBAL_DB
}

function database:global:test()
{
    database:global_load

    database:global_set test1 1
    test:run_cmd database:global_get test1
    test:should_be 1

    database:global_array_set test2 1 2222
    test:run_cmd database:global_array_get test2 1
    test:should_be 2222

    database:global_array_set test2 2 AAAA
    test:run_cmd database:global_array_get test2 2
    test:should_be AAAA

    database:global_set test3 0
    database:global_inc test3
    test:run_cmd database:global_get test3
    test:should_be 1

    database:global_save
}
test:register_test database:global:test
